package com.batchengine.system;

import java.util.Collection;

/**
 * Created by sunil.incubator@yahoo.com.
 * User: sunil.kalva
 * Date: Aug 13, 2009
 */
public class MemoryAlert {

    static Collection<Double> numbers = null;

    public MemoryAlert() {

        MemoryWarningSystem.setPercentageUsageThreshold(0.1);
        MemoryWarningSystem mws = new MemoryWarningSystem();
        mws.addListener(new MyMemoryHandler());
    }

    public static void main(String[] args) {

        MemoryWarningSystem.dispJavaMBeans();
//        MemoryWarningSystem.setPercentageUsageThreshold(0.2);
//        MemoryWarningSystem mws = new MemoryWarningSystem();
//        mws.addListener(new MyMemoryHandler());
//
//        numbers = new LinkedList<Double>();
//        while (true) {
//            numbers.add(Math.random());
//        }
    }

    static class MyMemoryHandler implements MemoryWarningSystem.Listener {

        public void memoryUsageLow(long usedMemory, long maxMemory) {

            System.out.println( "\n\n\n\n\nMemory usage low!!!\n\n\n\n\n\n");
            double percentageUsed = ((double) usedMemory) / maxMemory;
            System.out.println("percentageUsed = " + percentageUsed);
            MemoryWarningSystem.setPercentageUsageThreshold(0.8);
            System.out.println("size = " + numbers.size());
        }
    }
}


